// SpringTail : Simple Logging Abstraction block
// Copyright (c) 2008 Michael Saunders <mike@mikesaunders.com>
// All rights reserved.
// Released under Apache License 2.0 <www.apache.org/licenses>
// 
using System;

namespace SpringTail
{
	public class Logger
	{
		private readonly string _name;
		private readonly object _owner;

		public Logger(string name)
		{
			if (string.IsNullOrEmpty(name))
				throw new ArgumentNullException("name");
			_name = name;
		}

		public Logger(string name, object owner)
		{
			if (string.IsNullOrEmpty(name))
				throw new ArgumentNullException("name");
			_name = name;
			_owner = owner;
		}

		public static Action<LogMessage> LogAction { get; set; }

		private static void Log(LogMessage message)
		{
			if (LogAction == null)
				return;

			LogAction(message);
		}

		public string Name
		{
			get { return _name; }
		}

		public object Owner
		{
			get { return _owner; }
		}

		public void Log(LogLevel logLevel, string message)
		{
			Log(logLevel, message, null);
		}

		public void Debug(string message)
		{
			Log(LogLevel.Debug, message);
		}

		public void Info(string message)
		{
			Log(LogLevel.Info, message);
		}

		public void Warn(string message)
		{
			Log(LogLevel.Warn, message);
		}

		public void Error(string message)
		{
			Log(LogLevel.Error, message);
		}

		public void Fatal(string message)
		{
			Log(LogLevel.Fatal, message);
		}

		public void Log(LogLevel logLevel, string message, object data)
		{
			Log(new LogMessage(logLevel, Name, Owner, message, data));
		}

		public void Debug(string message, object data)
		{
			Log(LogLevel.Debug, message, data);
		}

		public void Info(string message, object data)
		{
			Log(LogLevel.Info, message, data);
		}

		public void Warn(string message, object data)
		{
			Log(LogLevel.Warn, message, data);
		}

		public void Error(string message, object data)
		{
			Log(LogLevel.Error, message, data);
		}

		public void Fatal(string message, object data)
		{
			Log(LogLevel.Fatal, message, data);
		}

		public void LogException(LogLevel logLevel, string message, Exception exception)
		{
			LogException(logLevel, message, null, exception);
		}

		public void DebugException(string message, Exception exception)
		{
			LogException(LogLevel.Debug, message, exception);
		}

		public void InfoException(string message, Exception exception)
		{
			LogException(LogLevel.Info, message, exception);
		}

		public void WarnException(string message, Exception exception)
		{
			LogException(LogLevel.Warn, message, exception);
		}

		public void ErrorException(string message, Exception exception)
		{
			LogException(LogLevel.Error, message, exception);
		}

		public void FatalException(string message, Exception exception)
		{
			LogException(LogLevel.Fatal, message, exception);
		}

		public void LogException(LogLevel logLevel, string message, object data, Exception exception)
		{
			if (exception == null)
				throw new ArgumentNullException("exception");
			Log(new LogMessage(logLevel, Name, Owner, message, data, exception));
		}

		public void DebugException(string message, object data, Exception exception)
		{
			LogException(LogLevel.Debug, message, data, exception);
		}

		public void InfoException(string message, object data, Exception exception)
		{
			LogException(LogLevel.Info, message, data, exception);
		}

		public void WarnException(string message, object data, Exception exception)
		{
			LogException(LogLevel.Warn, message, data, exception);
		}

		public void ErrorException(string message, object data, Exception exception)
		{
			LogException(LogLevel.Error, message, data, exception);
		}

		public void FatalException(string message, object data, Exception exception)
		{
			LogException(LogLevel.Fatal, message, data, exception);
		}
	}
}